Page({
  data: {
    rewardsList: []
  },

  onLoad: function (event) {
    // 获取当前用户的所有奖券
    var that = this
    wx.request({
      url: getApp().globalData.commonIP + '/rewardsDetail/getRewardsByNickName',
      method: 'GET',
      header: {
        'content-type': 'application/json', // 默认值
        'token': wx.getStorageSync('token')
      },
      data: {
        nickName: wx.getStorageSync('nickName')
      },
      success: res => {
        if (res.data.msg == '操作成功') {
          that.setData({
            rewardsList: res.data.data
          })
        } else {
          wx.showModal({
            title: '提示',
            content: res.data.msg,
            showCancel: false,
            complete: (res) => {
              if (res.cancel) {

              }

              if (res.confirm) {
                // 重置全局变量和去除缓存
                getApp().globalData.wxusername = ''
                getApp().globalData.wxuseravatar = ''
                wx.removeStorageSync('nickName')
                wx.removeStorageSync('avatarUrl')
                wx.removeStorageSync('token')

                wx.switchTab({
                  url: '/pages/index/index'
                })
              }
            }
          })
        }
      }
    })
  },

  getTheReward: function (event) {
    const state = event.currentTarget.dataset.rewardsState
    var that = this
    if (state == '未领取（点击领取）')
      wx.showModal({
        title: '提示',
        content: '确定要领取吗？',
        complete: (res) => {
          if (res.cancel) {
            return
          }

          if (res.confirm) {
            wx.showLoading({
              title: '正在加载...'
            });
            const id = event.currentTarget.dataset.id
            if (state == '未领取（点击领取）') {
              // 更新数据库
              wx.request({
                url: getApp().globalData.commonIP + '/rewardsDetail/updateRewardsStateById',
                method: 'PUT',
                header: {
                  'content-type': 'application/json', // 默认值
                  'token': wx.getStorageSync('token')
                },
                data: id,
                success: res => {
                  wx.hideLoading();
                  if (res.data.msg == '操作成功') {
                    const index = event.currentTarget.dataset.index;
                    that.setData({
                      [`rewardsList[${index}].rewardsState`]: '已领取',
                      [`rewardsList[${index}].rewardsImgUrl`]: '/images/icon/rewards-logo-on.png'
                    })
                  } else {
                    wx.showModal({
                      title: '提示',
                      content: res.data.msg,
                      complete: (res) => {
                        if (res.cancel) {}

                        if (res.confirm) {
                          // 重置全局变量和去除缓存
                          getApp().globalData.wxusername = ''
                          getApp().globalData.wxuseravatar = ''
                          wx.removeStorageSync('nickName')
                          wx.removeStorageSync('avatarUrl')
                          wx.removeStorageSync('token')

                          wx.switchTab({
                            url: '/pages/index/index'
                          })
                        }
                      }
                    })
                  }
                }
              })
            }
          }
        }
      })
  }
})